<%@ page session="false" %>
<%@ page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="javax.jcr.*, org.apache.sling.api.resource.ResourceUtil, org.apache.sling.api.resource.Resource, org.apache.commons.lang.WordUtils"%>
<%@ taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %>
<%@ page trimDirectiveWhitespaces="true" %>
<sling:defineObjects />

<%
	// Current node is of type verse
	String sid = currentNode.getProperty("sid").getValue().getString();

	// Find next and prev node
	Node prevNode = null;
	Node nextNode = null;
	Node current = null;
	
	NodeIterator ni = currentNode.getParent().getNodes();
	while(ni.hasNext()){
		current = ni.nextNode();
		String currentSid = current.getProperty("sid").getValue().getString();
		
		if(sid.equals(currentSid)){
			// Current node found
			if(ni.hasNext()){
				nextNode = ni.nextNode();
			}
			break;
		}
		prevNode = current;
	}
	
	String score = (String)request.getAttribute("score");
	if(score==null)
		score = "0";
	
	String[] splits = sid.split("/");
	String label = WordUtils.capitalize(splits[1])+". "+splits[2]+":"+splits[3];
	
	int firstSlashIndex = sid.indexOf('/');
	String idWithoutBible = sid.substring(firstSlashIndex+1);
	String referencePath = "/content/xrefs/" + idWithoutBible;
	Resource reference = resourceResolver.getResource(referencePath);
	String content = currentNode.getProperty("content").getValue().getString();
	
	String next = nextNode == null ? "" : nextNode.getProperty("sid").getValue().getString();
	String prev = prevNode == null ? "" : prevNode.getProperty("sid").getValue().getString();
%>

{
	id: "<%=sid%>", 
	content: "<%=content%>",
	label: "<%=label%>",
	score: "<%=score%>",
	next: "<%=next%>",
	prev: "<%=prev%>",
	references: <%if(reference != null){%>"<sling:include flush="true" path="<%=referencePath%>"/>"<%}else{%>""<%}%>
}